//
//  JWConfigureControls.m
//  KotH
//
//  Created by James Weatherley on 07/01/2010.
//  Copyright 2010 James Weatherley. All rights reserved.
//

#import "JWConfigureControls.h"
#import "wolf3dAppDelegate.h"
#import "OpenFeint.h"
#import "../koth/KothConstants.h"

@implementation JWConfigureControls

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	[sensitivity setValue:kothSwipeSensitivity animated:NO];
	
	if(kothSwipeMode == JWSwipeCamera) {
		dragCamera.on = YES;
		dragLandscape.on = NO;
	} else {
		dragCamera.on = NO;
		dragLandscape.on = YES;
	}
	
	if(kothVirtualJoystick) {
		joystick.on = YES;
	} else {
		joystick.on = NO;
	}
	
	invertY.on = kothInvertY;
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark Actions
-(IBAction)joystick:(UISwitch*)sender
{
	if(sender.on) {
		kothVirtualJoystick = YES;
	} else {
		kothVirtualJoystick = NO;
	}
}

-(IBAction)dragLandscape:(UISwitch*)sender
{
	if(sender.on == YES) {
		kothSwipeMode = JWSwipeLandscape;
		dragCamera.on = NO;
	} else {
		kothSwipeMode = JWSwipeCamera;
		dragCamera.on = YES;
	}
}

-(IBAction)dragCamera:(UISwitch*)sender
{
	if(sender.on == YES) {
		kothSwipeMode = JWSwipeCamera;
		dragLandscape.on = NO;
	} else {
		kothSwipeMode = JWSwipeLandscape;
		dragLandscape.on = YES;
	}
}

-(IBAction)invertY:(UISwitch*)sender
{
	kothInvertY = sender.on;
}

-(IBAction)sensitivity:(UISlider*)sender
{
	kothSwipeSensitivity = sender.value;
}

-(IBAction)done:(id)sender
{
	[JWConfigureControls saveControlConfiguration];
	[[OpenFeint class] performSelector:@selector(dismissRootController)];
}

-(IBAction)tryControls:(id)sender
{
	[[OpenFeint class] performSelector:@selector(dismissRootController)];
	wolf3dAppDelegate* delegate = (wolf3dAppDelegate*)[UIApplication sharedApplication].delegate;
	[delegate tryControls];
}

#pragma mark Persistence
+(void)saveControlConfiguration
{
	NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:[NSNumber numberWithFloat:kothSwipeSensitivity] forKey:@"swipeSensitivity"];
	[defaults setObject:[NSNumber numberWithInt:kothSwipeMode] forKey:@"swipeMode"];
	[defaults setObject:[NSNumber numberWithInt:kothInvertY] forKey:@"invertY"];
	[defaults setObject:[NSNumber numberWithInt:kothVirtualJoystick] forKey:@"joystick"];
	[defaults synchronize];
}

+(void)loadControlConfiguration
{
	NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
	NSNumber* swipeSensitivity = [defaults objectForKey:@"swipeSensitivity"];
	NSNumber* swipeMode = [defaults objectForKey:@"swipeMode"];
	NSNumber* inverted = [defaults objectForKey:@"invertY"];
	NSNumber* virtualJoystick = [defaults objectForKey:@"joystick"];
	
	if(!virtualJoystick) {
		// Player hasn't used 1.0.2 yet.
		// Default to the new non-virtual joystick mode - it's better.
		kothVirtualJoystick = NO;
		kothSwipeMode = JWSwipeLandscape;
		kothInvertY = NO;
		kothSwipeSensitivity = 4.0f;
		[JWConfigureControls saveControlConfiguration];
	} else {
		kothVirtualJoystick = [virtualJoystick boolValue];
		
		if(swipeSensitivity) {
			kothSwipeSensitivity = [swipeSensitivity floatValue];
		}
		
		if(swipeMode) {
			kothSwipeMode = (JWSwipeMode)[swipeMode intValue];
		}
		
		if(inverted) {
			kothInvertY = [inverted boolValue];
		}
	}
	
}

@end
